Ext.define('DragonApp.view.user.Controller', {
    extend: 'DragonApp.view.foundation.ViewController',

    alias: 'controller.user',

    requires: [
        'DragonApp.util.Msg'
    ],

    createDialog: function(record) {
        var me = this,
            view = me.getView();

        me.dialog = view.add({
            xtype: 'user_window',
            height: 264,
            width: 400,
            viewModel: {
                data: {
                    title: record ? '编辑' : '新建'
                }
            }
        });
        if (!Ext.isEmpty(record)) {
            me.dialog.down("form").loadRecord(record);
        }
        me.dialog.show();
    },

    onSave: function(button, e, options) {

        var window = this.lookupReference('user_window');

        var form = button.up('window').down('form').getForm();
        var formValue = form.getValues();

        //添加、编辑
        var url = DragonApp.ux.config.Config.getUrl(Ext.isEmpty(formValue.id)? '/index/Account/add/' : '/index/Account/update/');

        var me = this;
        var store = Ext.data.StoreManager.lookup('jsonPUserStore');

            form.submit({
                clientValidation: true,
                url: url,
                method:'POST',
                waitTitle:"请稍候",
                waitMsg:'正在提交...',
                success: function(form, action) {

                    DragonApp.util.Msg.showToast(action.result.msg);
                    window.destroy();
                    store.loadPage(1);
                },
                failure: function(form, action) {
                    switch (action.failureType) {
                        case Ext.form.action.Action.CLIENT_INVALID:
                            DragonApp.util.Msg.showToast('表单字段验证错误');
                            break;
                        case Ext.form.action.Action.CONNECT_FAILURE:
                            DragonApp.util.Msg.showToast('Ajax 通信失败');
                            break;
                        case Ext.form.action.Action.SERVER_INVALID:
                           DragonApp.util.Msg.showToast(action.result.msg);
                   }
                }
            });
    },

    //删除用户
    onDelete: function(button, e, options) {

        var record = button.getWidgetRecord();
        var formValue = {};

        formValue.id = record.get('id');

        var url = DragonApp.ux.config.Config.getUrl('/index/Account/delete/');
        var store = Ext.data.StoreManager.lookup('jsonPUserStore');

        Ext.Msg.show({
            title: '提示',
            msg: '删除这条记录?',
            buttons: Ext.Msg.YESNO,
            icon: Ext.Msg.QUESTION,
            fn: function(buttonId) {

                if (buttonId == 'yes') {

                    Ext.data.JsonP.request({
                        url: url,
                        async: false,
                        params: formValue,
                        callbackKey: "callback",
                        success: function(result) {

                            if (result.success) {
                                store.loadPage(1);
                            }
                            DragonApp.util.Msg.showToast(result.msg);
                        },
                        failure: function(result) {
                            Ext.Msg.alert('提示', result);
                        }
                    });
                }
            }
        });
    },

    //批量删除
    onBatDelete: function() {

        var grid = this.getView();
        var url = DragonApp.ux.config.Config.getUrl('/index/Account/batDelete/');

        this.onBatDeleteExec(grid,url);
    },
    //刷新
    onRefresh: function(button, e, options) {
        var store = Ext.data.StoreManager.lookup('jsonPUserStore');
        store.loadPage(1);
    },

    //用户封禁状态渲染
    statusRender: function(value) {
        switch (value) {
            case 1:
                return "正常";
            case 2:
                return "封禁";
        }

    }
});